#-*- codeing = utf-8 -*-
#@Time : 2020/9/9 20:58
#@Author : 阳某
#@File : 王者壁纸.py
#@Software : PyCharm

import requests
import re
from fake_useragent import UserAgent
import json
from urllib.parse import unquote
import os
from threading import Thread
headers = {
        'user-agent':UserAgent().chrome
    }
def ask_url(i):
    url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=27' \
          '35&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumC' \
          'lose=1&jsoncallback=jQuery171077178496362834_1599655711089&iAMSActivityId=51991&_eve' \
          'ryRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1599656283608'.format(i)
    response = requests.get(url,headers = headers)
    print(response.status_code)
    # print(response.text)
    data = re.findall('.*?\((.*?)\)',response.text,re.S)[0]
    data = json.loads(data)
    data = data['list']
    print(data)
    print(type(data))
    return data
def deal_data(data):
    name = data['sProdName']
    name = unquote(name)
    img_url = data['sProdImgNo_8']
    img_url = unquote(img_url)
    img_url = re.findall('(.*?)/\d+',img_url,re.S)[0]
    img_url = img_url + '/0'
    res = requests.get(img_url,headers = headers)
    filename = path + '/'+ name +'.jpg'
    with open(filename,'wb')as f:
        f.write(res.content)
path = '王者壁纸1920-1400'
if not os.path.exists(path):
    os.mkdir(path)
if __name__ == '__main__':
    listt = []
    for i in range(1):
        ask_url(i)
        data = ask_url(i)
        deal_data(data)
        t = Thread(target=ask_url,args=(i,))
        t.start()
        listt.append(t)
    for t in listt:
        t.join()